#!/usr/bin/env python
#
# Mechanisms for producing simulator VO files
#
# Author:   Mike Murphy <mamurph@cs.clemson.edu>
# Revision: 23 September 2009
#
#   Copyright 2009 Clemson University
#
#   Licensed under the Apache License, Version 2.0 (the "License");
#   you may not use this file except in compliance with the License.
#   You may obtain a copy of the License at
#
#       http://www.apache.org/licenses/LICENSE-2.0
#
#   Unless required by applicable law or agreed to in writing, software
#   distributed under the License is distributed on an "AS IS" BASIS,
#   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#   See the License for the specific language governing permissions and
#   limitations under the License.
#
# This material is based upon work supported under a National Science
# Foundation Graduate Research Fellowship. Any opinions, findings,
# conclusions or recommendations expressed in this publication are those of
# the author(s) and do not necessarily reflect the views of the National
# Science Foundation.

'''
Implements a uniform format writer for Virtual Organization list files, using
a text format readable by the vo_format_reader core module. The actual text
format is documented externally.
'''

from common_writer import CommonWriter

# Be sure to increment this number when changing the output format!
VO_FORMAT = 0.90
HEADER = 'VO list created by vo_format_writer'

class VOWriter(CommonWriter):
   '''
   Mechanism for writing VO list files in the format required by the core
   vo_format_reader module. Writing a VO list is a two-step process: first,
   the time at which a VO appears on the grid is written using write_time(),
   then the list of VOs is written using calls to write_vo(). It is important
   to note that all calls to write_time() must be in non-descending time
   order.
   '''
   def __init__(self, fh, normalize_time=False):
      '''
      @param fh: Open file handle (w mode) to which list will be written
      @param normalize_time: if True, times will be normalized relative to
                             the first time value written
      @type normalize_time: bool
      '''
      CommonWriter.__init__(self, fh, VO_FORMAT, HEADER, normalize_time)
   def write_vo(self, vo, **kwds):
      '''
      Writes a single VO record to the output list. A call to write_time()
      MUST proceed the first call to this method.

      @param vo: Name of the Virtual Organization
      @type vo: str
      @param kwds: Extra VO data (names and values must NOT contain
                     whitespace or colons)
      '''
      self.check_time()
      self.fh.write('$ %s' % str(vo))
      for key in kwds:
         value = str(key)
         if (len(key.split()) != 1) or (len(value.split()) != 1):
            raise Exception('Keys and values may not contain whitespace')
         if (':' in key) or (':' in value):
            raise Exception('Keys and values may not contain colons')
         self.fh.write(' %s:%s' % (key, value))
      self.fh.write('\n')
#
